export const recordAudio = () => {

    return new Promise<Blob>((resolve, reject) => {
        navigator.mediaDevices.getUserMedia({ audio: true })
          .then((stream) => {
                const mediaRecorder = new MediaRecorder(stream);
                const chunks: BlobPart[] = [];

                mediaRecorder.ondataavailable = (event) => {
                    if (event.data.size > 0) {
                        chunks.push(event.data);
                    }
                };

                mediaRecorder.onstop = () => {
                    const blob = new Blob(chunks, { type: 'audio/wav' });
                    resolve(blob);
                };
              

                mediaRecorder.onerror = (error) => {
                    reject(error);
                };

                mediaRecorder.start();

                return () => {
                    mediaRecorder.stop();
                    stream.getTracks().forEach((track) => track.stop());
                };
            })
          .catch((error) => {
                reject(error);
            });
    });
};